<template>
    <div class="edu-login-warp">
        <div class="login-form">
            <div class="form-logo"/>
            <el-form ref="loginForm" :model="loginForm" :rules="rules" class="edu-login-form">
                <el-form-item prop="userName">
                    <el-input
                        v-model="loginForm.userName"
                        type="text"
                        placeholder="请输入账号"
                        @keyup.enter.native="login"
                    />
                </el-form-item>
                <el-form-item prop="password">
                    <el-input
                        v-model="loginForm.password"
                        type="password"
                        placeholder="请输入密码"
                        @keyup.enter.native="login"
                        onpaste="return false" oncopy="return false" oncut="return false"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" class="login-btn" size="primary" @click="login">登录</el-button>
                </el-form-item>
            </el-form>
            <div class="sso-login-box">
                <a href="javascript:void(0)" @click="toSSOLogin">统一身份认证登录</a>
            </div>
        </div>
    </div>
</template>

<script>
import { getSessionDataByLocal } from "common/src/api/session";
import utils from "common/src/utils/utils";
import md5 from 'js-md5';
export default {
    name: "Login",
    data() {
        var validateAccount = (rule, value, callback) => {
            var regex = /^[\w\d]{6,10}$/;
            // debugger;
            if (regex.test(value)) {
                callback();
            } else {
                callback(new Error("请输入字母或数字(6~10位)."));
            }
        };
        return {
            loginForm: {
                userName: "",
                password: ""
            },
            rules: {
                userName: [
                    {
                        required: true,
                        message: "请输入账号",
                        trigger: "blur"
                    },
                    {
                        validator: validateAccount,
                        trigger: "blur"
                    }
                ],
                password: [
                    {
                        required: true,
                        message: "请输入密码",
                        trigger: "blur"
                    }
                ]
            }
        };
    },
    mounted() {
        let userInfo = JSON.parse(localStorage.getItem("user"));
        if (!userInfo) {
            userInfo = {
                uid: ""
            };
        }
        this.loginForm.userName = userInfo.uid;
    },
    methods: {
        login() {
            this.$refs.loginForm.validate(valid => {
                if (valid) {
                    this.loginForm.password = md5(md5(this.loginForm.password));
                    getSessionDataByLocal(this.loginForm).then(res => {
                        if (res.code == 200) {
                            //将登录状态保存在sessionStorage中，在全局路由守卫中进行判断，
                            //如果从sessionStorage中获取不到登录状态，则为用户打开浏览器后第一次登录
                            sessionStorage.setItem("isLogin", "isLogin");
                            this.$store.commit("set_session_data", res.data);
                            const targetUrl = utils.getCookie("target_url");
                            if (this.authenticate(targetUrl)) {
                                this.$router.replace(targetUrl);
                                utils.setCookie("target_url", "", -1);
                            } else this.$router.replace("/workbench");
                        } else if (res.code == 305) {
                            this.$message({
                                type: "error",
                                message: "用户名或密码错误，请重新输入."
                            });
                            this.loginForm.password = "";
                        } else if (res.code == 308) {
                            this.$message({
                                type: "error",
                                message: "本地账户不允许登录."
                            });
                            this.loginForm.password = "";
                        } else if (res.code == 307) {
                            this.$message({
                                type: "error",
                                message:
                                    "此用户已锁定，请次日登录或联系管理员重置密码."
                            });
                            this.loginForm.password = "";
                        } else if (res.code == 311) {
                            this.$message({
                                type: "error",
                                message:
                                    "系统已达最大登录数量，请稍后再试."
                            });
                            this.loginForm.password = "";
                        } else if (res.code == 312) {
                            this.$message({
                                type: "error",
                                message:
                                    "服务未注册完成，请稍后再试."
                            });
                            this.loginForm.password = "";
                        } else{
                            this.loginForm.password = "";
                            throw res.code;
                        }
                    });
                } else return false;
            });
        },
        authenticate(targetUrl) {
            if (!targetUrl) return false;
            const auths = this.$store.state.session.auths;
            return auths.some((item, index, arr) => {
                return item.urlPath == null
                    ? false
                    : item.urlPath.toLowerCase() == targetUrl.toLowerCase();
            });
        },
        toSSOLogin() {
            window.location.href = this.$store.state.ssourl;
        }
    }
};
</script>

<style lang="scss" scoped>
.sso-login-box {
    text-align: center;
}
.sso-login-box a {
    display: inline;
}
</style>

